package com.sjr.easy.chat.app.controller;

import com.alibaba.fastjson2.JSON;
import com.sjr.easy.chat.app.constants.RabbitMQConstants;
import com.sjr.easy.chat.app.dto.AppTokenInfo;
import com.sjr.easy.chat.app.utils.AppTokenManager;
import com.sjr.easy.chat.app.utils.SequenceUtils;
import com.sjr.easy.chat.business.ConversationBusiness;
import com.sjr.easy.chat.dto.Result;
import com.sjr.easy.chat.model.Conversation;
import com.sjr.easy.chat.model.InboxMessage;
import com.sjr.easy.chat.service.ChatGroupUserService;
import com.sjr.easy.chat.service.ContactService;
import com.sjr.easy.chat.service.InboxMessageService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
public class AppInboxMessageController {

    @Autowired
    private AppTokenManager appTokenManager;

    @Autowired
    private ConversationBusiness conversationBusiness;

    @Autowired
    private ChatGroupUserService chatGroupUserService;

    @Autowired
    private ContactService contactService;

    @Autowired
    private InboxMessageService inboxMessageService;

    @Autowired
    private SequenceUtils sequenceUtils;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Data
    static class AppSendMessageReq {
        @ApiModelProperty("")
        private Long targetUserId;

        @ApiModelProperty("聊天群ID")
        private Long chatGroupId;

        @ApiModelProperty("会话类型")
        private Integer conversationType;

        @ApiModelProperty("0 文字消息")
        private Long messageType;

        @ApiModelProperty("消息内容")
        private String message;
    }

    @ApiOperation("发送消息")
    @PostMapping("/app/inboxMessage/sendMessage")
    public Result<InboxMessage> sendMessage(@RequestBody AppSendMessageReq appSendMessageReq) {
        log.info("sendMessageReq:{}", appSendMessageReq);
        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        Long selfUserId = tokenInfo.getId();
        Long friendUserId = appSendMessageReq.targetUserId;
        Long chatGroupId = appSendMessageReq.chatGroupId;
        Integer conversationType = appSendMessageReq.conversationType;

        Assert.isTrue(conversationType == 1 || conversationType == 2, "会话类型参数异常");
        if (conversationType == 1 && friendUserId == null) {
            log.error("好友ID不允许为空");
            return Result.error("好友ID不允许为空");
        }
        if (conversationType == 2 && chatGroupId == null) {
            log.error("群ID不允许为空");
            return Result.error("群ID不允许为空");
        }

        Conversation conversation = conversationBusiness.getOrCreateConversation(
                conversationType, selfUserId, friendUserId, chatGroupId);
        InboxMessage inboxMessage = new InboxMessage();
        inboxMessage.setId(sequenceUtils.genInboxMessageId());
        inboxMessage.setConversationId(conversation.getId());
        inboxMessage.setSourceUserId(tokenInfo.getId());
        if (conversationType == 1) {
            inboxMessage.setTargetUserId(appSendMessageReq.targetUserId);
            inboxMessage.setChatGroupId(null);
        }
        if (conversationType == 2) {
            inboxMessage.setTargetUserId(null);
            inboxMessage.setChatGroupId(chatGroupId);
        }
        inboxMessage.setMessageType(appSendMessageReq.messageType);
        inboxMessage.setMessage(appSendMessageReq.message);
        inboxMessage.setSendTime(new Date());
        inboxMessage.setStatus(1);
        inboxMessageService.addInboxMessage(inboxMessage);

        rabbitTemplate.convertAndSend(RabbitMQConstants.NewInboxMessageExchange, "",
                JSON.toJSONString(inboxMessage));
        return Result.success(inboxMessage);
    }

}
